package cn.xorange.commons.core.basic.service;

import cn.xorange.commons.data.core.entity.BaseEntity;
import cn.xorange.commons.data.core.mapper.MBaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

public class BaseBatchService {

    @Autowired
    MBaseMapper<BaseEntity> MBaseMapper;

    @Transactional(rollbackFor = RuntimeException.class)
    public Integer insertBathMap(String tableName, List<Map<String,Object>> mapList){
        int count = mapList.size();
        int insertLength = count;
        int i = 0;
        while (insertLength>500){
            MBaseMapper.insertBathMap(tableName,mapList.subList(i,i+500));
            i = i + 500;
            insertLength = insertLength - 500;
        }
        if(insertLength>0){
            MBaseMapper.insertBathMap(tableName,mapList.subList(i,i+insertLength));
        }
        return count;
    }
}
